// AWTBorderLayout.cpp: implementation of the AWTBorderLayout class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
//#include "AWTTest.h"
#include "AWTBorderLayout.h"

#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif

IMPLEMENT_DYNAMIC(AWTBorderLayout, AWTLayout)

AWTBorderLayout::AWTBorderLayout()
{
	m_hgap = 0;	
	m_vgap = 0;	
	m_north = NULL;
	m_south = NULL;
	m_east = NULL;
	m_west = NULL;
	m_center = NULL;
}

AWTBorderLayout::AWTBorderLayout(int hgap, int vgap)
{
	m_hgap = hgap;	
	m_vgap = vgap;	
	m_north = NULL;
	m_south = NULL;
	m_east = NULL;
	m_west = NULL;
	m_center = NULL;
}

AWTBorderLayout::~AWTBorderLayout()
{
}

void AWTBorderLayout::addLayoutComponent(CString* nm, AWTComponent* comp)
{
	CString name;
	if (nm == NULL) 
		name = "Center";
	else
		name = *nm;
	if (name.CompareNoCase (_T("Center")) == 0)
		m_center = comp;
	else if (name.CompareNoCase (_T("North")) == 0)
		m_north = comp;
	else if (name.CompareNoCase (_T("South")) == 0)
		m_south = comp;
	else if (name.CompareNoCase (_T("East")) == 0)
		m_east = comp;
	else if (name.CompareNoCase (_T("West")) == 0)
		m_west = comp;
}

void AWTBorderLayout::removeLayoutComponent(AWTComponent* comp)
{
	if (comp == m_center)
		m_center = NULL;
	else if (comp == m_north)
		m_north = NULL;
	else if (comp == m_south)
		m_south = NULL;
	else if (comp == m_east)
		m_east = NULL;
	else if (comp == m_west)
		m_west = NULL;
}

CSize AWTBorderLayout::preferredLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);

	if ((m_east != NULL) && m_east->isVisible()) {
	    CSize d = m_east->getPreferredSize();
	    dim.cx += d.cx + m_hgap;
	    dim.cy = max(d.cy, dim.cy);
	}
	if ((m_west != NULL) && m_west->isVisible()) {
	    CSize d = m_west->getPreferredSize();
	    dim.cx += d.cx + m_hgap;
	    dim.cy = max(d.cy, dim.cy);
	}
	if ((m_center != NULL) && m_center->isVisible()) {
	    CSize d = m_center->getPreferredSize();
	    dim.cx += d.cx;
	    dim.cy = max(d.cy, dim.cy);
	}
	if ((m_north != NULL) && m_north->isVisible()) {
	    CSize d = m_north->getPreferredSize();
	    dim.cx = max(d.cx, dim.cx);
	    dim.cy += d.cy + m_vgap;
	}
	if ((m_south != NULL) && m_south->isVisible()) {
	    CSize d = m_south->getPreferredSize();
	    dim.cx = max(d.cx, dim.cx);
	    dim.cy += d.cy + m_vgap;
	}

	AWTInsets insets = target->getInsets();
	dim.cx += insets.left + insets.right;
	dim.cy += insets.top + insets.bottom;

	return dim;
}

CSize AWTBorderLayout::minimumLayoutSize(AWTContainer* target)
{
	CSize dim(0,0);

	if ((m_east != NULL) && m_east->isVisible()) 
	{
	  CSize d = m_east->getMinimumSize();
	  dim.cx += d.cx + m_hgap;
	  dim.cy = max(d.cy, dim.cy);
	}
	if ((m_west != NULL) && m_west->isVisible()) {
	    CSize d = m_west->getMinimumSize();
	    dim.cx += d.cx + m_hgap;
	    dim.cy = max(d.cy, dim.cy);
	}
	if ((m_center != NULL) && m_center->isVisible()) {
	    CSize d = m_center->getMinimumSize();
	    dim.cx += d.cx;
	    dim.cy = max(d.cy, dim.cy);
	}
	if ((m_north != NULL) && m_north->isVisible()) {
	    CSize d = m_north->getMinimumSize();
	    dim.cx = max(d.cx, dim.cx);
	    dim.cy += d.cy + m_vgap;
	}
	if ((m_south != NULL) && m_south->isVisible()) {
	    CSize d = m_south->getMinimumSize();
	    dim.cx = max(d.cx, dim.cx);
	    dim.cy += d.cy + m_vgap;
	}

	AWTInsets insets = target->getInsets();
	dim.cx += insets.left + insets.right;
	dim.cy += insets.top + insets.bottom;

	return dim;
}

void AWTBorderLayout::layoutContainer(AWTContainer* target)
{
	AWTInsets insets = target->getInsets();
	int top = insets.top;
	int bottom = target->getHeight() - insets.bottom;
	int left = insets.left;
	int right = target->getWidth() - insets.right;

	if ((m_north != NULL) && m_north->isVisible()) 
	{
	    m_north->setSize(right - left, m_north->getHeight());
	    CSize d = m_north->getPreferredSize();
	    m_north->setBounds(left, top, right - left, d.cy);
	    top += d.cy + m_vgap;
	}
	if ((m_south != NULL) && m_south->isVisible()) {
	    m_south->setSize(right - left, m_south->getHeight());
	    CSize d = m_south->getPreferredSize();
	    m_south->setBounds(left, bottom - d.cy, right - left, d.cy);
	    bottom -= d.cy + m_vgap;
	}
	if ((m_east != NULL) && m_east->isVisible()) {
	    m_east->setSize(m_east->getWidth(), bottom - top);
	    CSize d = m_east->getPreferredSize();
	    m_east->setBounds(right - d.cx, top, d.cx, bottom - top);
	    right -= d.cx + m_hgap;
	}
	if ((m_west != NULL) && m_west->isVisible()) {
	    m_west->setSize(m_west->getWidth(), bottom - top);
	    CSize d = m_west->getPreferredSize();
	    m_west->setBounds(left, top, d.cx, bottom - top);
	    left += d.cx + m_hgap;
	}
	if ((m_center != NULL) && m_center->isVisible()) {
	    m_center->setBounds(left, top, right - left, bottom - top);
	}
}
